package com.jsy.mini12306.manage.service.impl;

import com.jsy.mini12306.common.entity.Ticket;
import com.jsy.mini12306.common.entity.TrainCalendar;
import com.jsy.mini12306.common.entity.TrainPassStation;
import com.jsy.mini12306.common.entity.TrainSeat;
import com.jsy.mini12306.manage.bo.TrainBO;
import com.jsy.mini12306.manage.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.function.Consumer;

/**
 * @author: SongyangJi
 * @description:
 * @since: 2021/9/3
 */
@Slf4j
@Service("manageTrainService")
public class ManageTrainServiceImpl extends TrainServiceImpl implements ManageTrainService {

    @Resource
    TrainService trainService;

    @Resource
    TrainSeatService trainSeatService;

    @Resource
    TrainPassStationService trainPassStationService;

    @Resource
    TicketService ticketService;

    @Resource
    TrainCalendarService trainCalendarService;


    @Override
    @Transactional
    public void addTrainBO(TrainBO trainBO) {
        trainService.save(trainBO.getTrain());
        Integer trainId = trainBO.getTrain().getTrainId();
        trainBO.getTrainSeats().forEach(trainSeat -> trainSeat.setTrainId(trainId));
        trainBO.getTrainPassStations().forEach(trainPassStation -> trainPassStation.setTrainId(trainId));
        trainBO.getTickets().forEach(ticket -> ticket.setTrainId(trainId)
        );
        trainBO.getTrainCalendars().forEach(trainCalendar -> trainCalendar.setTrainId(trainId));
        trainSeatService.saveBatch(trainBO.getTrainSeats());
        trainPassStationService.saveBatch(trainBO.getTrainPassStations());
        ticketService.saveBatch(trainBO.getTickets());
        trainCalendarService.saveBatch(trainBO.getTrainCalendars());
    }

}
